import ImageGrab
import os
import time
import win32api, win32con
"""
CONSTANT DECLARATIONS
"""
#SCREEN SIZE
XSIZE = 800
YSIZE = 600
X1 = 560
X2 = X1 + XSIZE
Y1 = 60
Y2 = Y1 + YSIZE
#MAIN MENU
PLAYBOX_X = X1 + 400
PLAYBOX_Y = Y1 + 550
#CHAR SELECT
CHARBOX_X = X1 + 160
CHARBOX_Y = Y1 + 130
#ENTER REALM BUTTON
ENTERBOX_X = X1 + 700
ENTERBOX_Y = Y1 + 570
#INVENTORY ICONS
BOX_SIZE = 40
BOX_OFFSET = 4
BOX1_X = X1 + 635
BOX1_Y = Y1 + 430
#PICKUP ICONS
#THE INVENTORY IS JUST THE PICKUP, OFF BY 100 PIXELS
PICKUP_OFFSET = 100

#STATES:
#  0 = pregame
#  1 = in the nexus
#  2 = in a realm
STATE = 0

def leftClick():
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,0,0)
    time.sleep(.1)
    win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,0,0)

def mousePos(cord):
    win32api.SetCursorPos((cord[0], cord[1]))

def screenGrab():
    """
    Grab a screenshot using the coordinates defined above.
    Will be used on Realm of the Mad God, running at 1920x1080 in Chrome
    on Windows 7, with no adjustments to screen position.
    Because fuck you, that's why.
    """
    box = ()
    im = ImageGrab.grab((X1, Y1, X2, Y2))
    im.save(os.getcwd() + '\\full_snap__' + str(int(time.time())) +
'.png', 'PNG')

def startGame():
    #click on play
    #run char select
    mousePos((PLAYBOX_X, PLAYBOX_Y))
    leftClick()
    leftClick()
    time.sleep(.1)
    charSelect()
    
def charSelect():
    #make sure we have char 1
    mousePos((CHARBOX_X, CHARBOX_Y))
    leftClick()
    #play!
    time.sleep(.1)
    mousePos((PLAYBOX_X, PLAYBOX_Y))
    leftClick()
    time.sleep(1)
    
def enterRealm():
    #move forward a bit
    win32api.keybd_event(win32api.VkKeyScan("W"), 0)
    time.sleep(5)
    win32api.keybd_event(win32api.VkKeyScan("W"), 0, KEYEVENTF_KEYUP)
    

def main():
    startGame()
    win32api.keybd_event(win32con.VK_TAB, 0, 0, 0)    
    win32api.keybd_event(win32con.VK_TAB, 0, 2, 0)    
    STATE = 1 #after startGame we should be...in the game.
    enterRealm()
    
    
    screenGrab()
 
if __name__ == '__main__':
    main()
    